<template>
  <div>
    <div class="mlt">
      <el-button
        v-permissions="{ permission: ['add:Group'] }"
        type="primary"
        @click="addRole"
      >
        添加职位
      </el-button>
    </div>
    <div class="mlt">
      <el-table :data="tableData" :empty-text="emptyText" style="width: 100%">
        <el-table-column label="职位" prop="group_name" />
        <el-table-column label="部门" prop="dept_name" />
        <el-table-column label="是否参加排班">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.is_partake == 'Y'">是</el-tag>
            <el-tag v-if="scope.row.is_partake == 'N'">否</el-tag>
            <div></div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              v-permissions="{ permission: ['edit:Group'] }"
              size="mini"
              type="primary"
              @click="updataPosition(scope.row)"
            >
              编辑
            </el-button>
            <el-button
              v-permissions="{ permission: ['delete:Group'] }"
              size="mini"
              type="danger"
              @click="delPosition(scope.row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      :before-close="handleClose"
      title="职位"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form ref="form" label-width="80px" :model="form">
        <el-form-item label="职位名称">
          <el-input v-model="form.group_name" />
        </el-form-item>
        <el-form-item label="部门">
          <el-select
            v-model="departmentValue"
            placeholder="请选择部门"
            @change="departmentChange"
          >
            <el-option
              v-for="item in departmentList"
              :key="item.id"
              :label="item.dept_name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否排班">
          <el-select
            v-model="SchedulingValue"
            placeholder="请选择"
            @change="SchedulingChange"
          >
            <el-option
              v-for="item in SchedulingList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="SubmitClick">确 定</el-button>
      </span>
    </el-dialog>
    <div v-if="total != ''">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
  import { getGroupList, addGroup, delGroup } from '@/api/group'
  import { getDeptLists } from '@/api/addOrganization'

  // import Jurisdiction from './components/jurisdiction.vue'
  export default {
    name: 'Reception',

    data() {
      return {
        emptyText: ' ',
        tableData: [],
        departmentValue: '',
        departmentList: [],
        dialogVisible: false,
        //切换页面状态
        flag: 0,
        form: {
          group_name: '',
          is_partake: '',
          dept_id: '',
        },
        optionData: [],
        id: '',
        page: 1,
        total: '',
        SchedulingValue: '',
        SchedulingList: [
          {
            value: 'Y',
            label: '参加排班',
          },
          {
            value: 'N',
            label: '不参加排班',
          },
        ],
      }
    },
    computed: {},
    watch: {},
    created() {},
    mounted() {
      this.init()
    },
    methods: {
      // 初始化
      init() {
        this.position()
      },
      // 获取岗位
      position() {
        const loading = this.$loading({
          lock: true,
          target:
            this.tableData.length == 0
              ? '.el-table__empty-block'
              : '.el-table__body',
        })
        const params = {
          pagesize: 10,
          page: this.page,
        }
        getGroupList(params).then((res) => {
          this.tableData = res.data.data
          this.total = res.data.total
          loading.close()
          if (res.data.data.length == 0) {
            this.emptyText = '暂无数据'
          }
        })
        getDeptLists().then((res) => {
          this.departmentList = res.data
        })
      },
      // 编辑角色
      updataPosition(e) {
        this.departmentValue = e.dept_id
        this.form.dept_id = e.dept_id
        this.form.id = e.id
        this.form.group_name = e.group_name
        this.form.is_partake = e.is_partake
        //

        // this.departmentList.forEach((i) => {
        //   if (i.mec_id == e.mec_id) {
        //     this.departmentValue = i.dept_name
        //   }
        // })

        this.SchedulingList.forEach((j) => {
          if (j.value == e.is_partake) {
            this.SchedulingValue = j.label
          }
        })
        this.dialogVisible = true
        // this.addRole()
      },
      showMain(e) {
        this.flag = e
      },
      // 删除岗位
      delPosition(e) {
        const params = {
          id: e,
        }
        delGroup(params).then(() => {
          this.init()
        })
      },
      // 添加职位
      addRole() {
        this.form = {
          group_name: '',
          is_partake: '',
          dept_id: '',
        }
        this.SchedulingValue = ''
        this.departmentValue = ''
        this.dialogVisible = true
      },
      // 部门添加
      departmentChange(e) {
        this.form.dept_id = e
      },
      // 是否参加排班
      SchedulingChange(e) {
        this.form.is_partake = e
        //
      },
      // 新增角色提交按钮
      SubmitClick() {
        const jy = {
          group_name: '请输入职位名称',
          is_partake: '请选择是否参加排班',
          dept_id: '请选择部门',
        }
        if (Object.keys(this.form).length === 0) {
          this.$message.error('请填写信息')
          return
        } else {
          for (var key in this.form) {
            if (this.form[key] == '') {
              this.$message.error(jy[key])
              return
            }
          }
        }
        addGroup(this.form).then(() => {
          this.init()
          this.departmentValue = ''
          this.SchedulingValue = ''
        })
        this.dialogVisible = false
      },
      handleCurrentChange(val) {
        const _this = this
        _this.page = val
        this.init()
      },
      // 关闭弹框的回调
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(() => {
            done()
          })
          .catch(() => {})
      },
    },
  }
</script>
<style></style>
